package mango.web.client.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import mango.web.client.View;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

public class AdminExportView extends View {
	protected static AdminExportView adminExportView;

	public static AdminExportView get(String key) {
		if ("".equals(key)) {
			if (AdminExportView.adminExportView == null) AdminExportView.adminExportView = new AdminExportView().init();
			return AdminExportView.adminExportView;
		}
		return null;
	}

	public HashMap<String, CheckBox> checkMap = new HashMap<String, CheckBox>();
	public List<String> exportList = new ArrayList<String>();
	public DialogBox dialogBox;

	public AdminExportView init() {

		this.panel.setSize("800px", "200px");

		AbsolutePanel panel;
		Label label;
		HTML html;
		TextBox textBox;
		CheckBox checkBox;
		ListBox listBox;
		RadioButton radioButton;
		TextArea textArea;
		Button button;
		int top;
		int left;

		panel = new AbsolutePanel();
		panel.setSize("800px", "200px");
		this.panel.add(panel, 0, 0);

		left = 120;
		top = 0;

		checkBox = new CheckBox(ContactVo._memberId);
		panel.add(checkBox, 0 * left, top);
		checkMap.put(ContactVo._memberId, checkBox);

		top = 30;

		checkBox = new CheckBox(ContactVo._contactId);
		panel.add(checkBox, 0 * left, top);
		checkMap.put(ContactVo._contactId, checkBox);

		checkBox = new CheckBox(ContactVo._firstname);
		panel.add(checkBox, 1 * left, top);
		checkMap.put(ContactVo._firstname, checkBox);

		checkBox = new CheckBox(ContactVo._surname);
		panel.add(checkBox, 2 * left, top);
		checkMap.put(ContactVo._surname, checkBox);

		checkBox = new CheckBox(ContactVo._BHPhone);
		panel.add(checkBox, 3 * left, top);
		checkMap.put(ContactVo._BHPhone, checkBox);

		checkBox = new CheckBox(ContactVo._AHPhone);
		panel.add(checkBox, 4 * left, top);
		checkMap.put(ContactVo._AHPhone, checkBox);

		top = 60;

		checkBox = new CheckBox(ContactVo._fax);
		panel.add(checkBox, 0 * left, top);
		checkMap.put(ContactVo._fax, checkBox);

		checkBox = new CheckBox(ContactVo._identifier);
		panel.add(checkBox, 1 * left, top);
		checkMap.put(ContactVo._identifier, checkBox);

		checkBox = new CheckBox(ContactVo._mobile);
		panel.add(checkBox, 2 * left, top);
		checkMap.put(ContactVo._mobile, checkBox);

		checkBox = new CheckBox(ContactVo._email);
		panel.add(checkBox, 3 * left, top);
		checkMap.put(ContactVo._email, checkBox);

		checkBox = new CheckBox(ContactVo._service);
		panel.add(checkBox, 4 * left, top);
		checkMap.put(ContactVo._service, checkBox);

		top = 90;

		checkBox = new CheckBox(ContactVo._businessStreet);
		panel.add(checkBox, 0 * left, top);
		checkMap.put(ContactVo._businessStreet, checkBox);

		checkBox = new CheckBox(ContactVo._businessCity);
		panel.add(checkBox, 1 * left, top);
		checkMap.put(ContactVo._businessCity, checkBox);

		checkBox = new CheckBox(ContactVo._businessState);
		panel.add(checkBox, 2 * left, top);
		checkMap.put(ContactVo._businessState, checkBox);

		checkBox = new CheckBox(ContactVo._businessCountry);
		panel.add(checkBox, 3 * left, top);
		checkMap.put(ContactVo._businessCountry, checkBox);

		checkBox = new CheckBox(ContactVo._businessPostCode);
		panel.add(checkBox, 4 * left, top);
		checkMap.put(ContactVo._businessPostCode, checkBox);

		top = 120;

		checkBox = new CheckBox(ContactVo._postalStreet);
		panel.add(checkBox, 0 * left, top);
		checkMap.put(ContactVo._postalStreet, checkBox);

		checkBox = new CheckBox(ContactVo._postalCity);
		panel.add(checkBox, 1 * left, top);
		checkMap.put(ContactVo._postalCity, checkBox);

		checkBox = new CheckBox(ContactVo._postalState);
		panel.add(checkBox, 2 * left, top);
		checkMap.put(ContactVo._postalState, checkBox);

		checkBox = new CheckBox(ContactVo._postalCountry);
		panel.add(checkBox, 3 * left, top);
		checkMap.put(ContactVo._postalCountry, checkBox);

		checkBox = new CheckBox(ContactVo._postalPostCode);
		panel.add(checkBox, 4 * left, top);
		checkMap.put(ContactVo._postalPostCode, checkBox);

		button = new Button("OK");
		button.setWidth("60px");
		panel.add(button, 300, 160);
		button.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				exportList = new ArrayList<String>();
				String exportStr = "";
				for (String key : checkMap.keySet()) {
					if (checkMap.get(key).getValue() == true) {
						exportList.add(key);
						exportStr += "^" + key;
					}
				}
				if (exportStr.startsWith("^")) exportStr = exportStr.substring(1);
				String url = "Export.jsp?key=" + exportStr;
				dialogBox.hide();
				Window.open(url, "_self", "");
			}
		});

		button = new Button("Cancel");
		button.setWidth("60px");
		panel.add(button, 370, 160);
		button.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				dialogBox.hide();
			}
		});

		// TODO:AdminSearchView
		this.dialogBox = new DialogBox();
		this.dialogBox.setHTML("<b style='color:red'>Export</b>");
		this.dialogBox.setModal(true);
		this.dialogBox.setWidget(this.panel);

		return this;
	}

	public AdminExportView reset() {
		for (String key : this.checkMap.keySet()) {
			this.checkMap.get(key).setValue(true);
		}
		this.dialogBox.setPopupPosition(80, 80);
		return this;
	}

	public AdminExportView view() {

		return this;
	}
}
